import grails.util.GrailsUtil

class BootStrap {

    def init = {servletContext ->

        Locale.setDefault(Locale.CHINA)

        if (GrailsUtil.environment == 'development') {
            ProductCategory allProduct = new ProductCategory(name: '所有产品与服务')

            ProductCategory photoPrintCategory = new ProductCategory(name: '照片打印')
            ProductCategory photoProductCategory = new ProductCategory(name: '相片相关产品')
            ProductCategory photoModificationCategory = new ProductCategory(name: '照片美化')

            allProduct.addChild(photoPrintCategory)
            allProduct.addChild(photoProductCategory)
            allProduct.addChild(photoModificationCategory)

            ProductCategory photoFrameCategory = new ProductCategory(name: '相框')
            ProductCategory photoAlbumCategory = new ProductCategory(name: '相册')

            photoPrintCategory.addChild(photoFrameCategory)
            photoPrintCategory.addChild(photoAlbumCategory)

            Product photoPrint1 = new Product(name: '丽晶5X7', currentPrice: 0.7, currentCost: 0.1)
            Product photoPrint2 = new Product(name: '丽晶4X6', currentPrice: 0.5, currentCost: 0.1)
            Product photoModification = new Product(name: '换背景', currentPrice: 10, currentCost: 2)
            PhotoFrame photoFrame1 = new PhotoFrame(name: '镁铝合金', currentPrice: 20, currentCost: 5, length: 20, width: 15.5)

            photoPrintCategory.addToProducts(photoPrint1)
            photoPrintCategory.addToProducts(photoPrint2)
            photoModificationCategory.addToProducts(photoModification)
            photoAlbumCategory.addToProducts(photoFrame1)

            saveModel(allProduct)

            Staff staffYang = new Staff(name: '杨亚刚', handPhone: "13774256612")
            Staff staffHu = new Staff(name: '胡丹', handPhone: "13774256612")
            StaffGroup managerGroup = new StaffGroup(name: '店长', role: Constants.ROLE_MANAGER)
            StaffGroup shopAssitantGroup = new StaffGroup(name: '店员', role: Constants.ROLE_SHOP_ASSISTANT)
            managerGroup.addToStaffs(staffYang)
            shopAssitantGroup.addToStaffs(staffHu)

            PercentageCommissionPolicy percentageCommissionPolicyPhotoFrame = new PercentageCommissionPolicy(name: '相框', productCategory: photoFrameCategory, commissionPercentage: 0.1)
            QuantityCommissionPolicy quantityCommissionPolicy1 = new QuantityCommissionPolicy(name: '修片', productCategory: photoPrintCategory, unitCommission: 0.2)
            QuantityCommissionPolicy quantityCommissionPolicy2 = new QuantityCommissionPolicy(name: '镁铝合金相框', product: photoFrame1, unitCommission: 2)

            CommissionPolicyGroup manageCPG = new CommissionPolicyGroup(name: '店长提成')
            CommissionPolicyGroup shopAssistantCPG = new CommissionPolicyGroup(name: '店员提成')
            shopAssistantCPG.addToCommissionPolicys(percentageCommissionPolicyPhotoFrame)
            shopAssistantCPG.addToCommissionPolicys(quantityCommissionPolicy1)
            shopAssistantCPG.addToCommissionPolicys(quantityCommissionPolicy2)

            saveModel(manageCPG)
            saveModel(shopAssistantCPG)

            manageCPG.addToStaffs(staffYang)
            shopAssistantCPG.addToStaffs(staffHu)

            saveModel(managerGroup)
            saveModel(shopAssitantGroup)


            Membership member = new Membership(name: "会员", discount: 0.8)
            Membership nonMember = new Membership(name: "非会员", discount: 1)

            Customer customer1 = new Customer(name: '刘超', handPhone: "13774256612", dob:new Date())
            Customer customer2 = new Customer(name: '胡碧秋', handPhone: "13774256612", cardNumber:'SN000001')
            nonMember.addToCustomers(customer1)
            member.addToCustomers(customer2)

            saveModel(member)
            saveModel(nonMember)

            //Auth user role
            AuthRole adminRole = new AuthRole(authority: Constants.ROLE_ADMIN, description: '管理员')
            AuthRole managerRole = new AuthRole(authority: Constants.ROLE_MANAGER, description: '店长')
            AuthRole saRole = new AuthRole(authority: Constants.ROLE_SHOP_ASSISTANT, description: '店员')
            AuthRole customerRole = new AuthRole(authority: Constants.ROLE_CUSTOMER, description: '顾客')
            AuthUser admin = new AuthUser(passwd: 'd033e22ae348aeb5660fc2140aec35850c4da997',
                    username: 'admin',
                    userRealName: '系统管理员',
                    enabled: true,
                    email: 'liuchao36@gmail.com',
                    emailShow: true
            )
            AuthUser manager = new AuthUser(passwd: 'd033e22ae348aeb5660fc2140aec35850c4da997',
                    username: 'yangyagang',
                    userRealName: '杨亚刚',
                    enabled: true,
                    email: 'liuchao36@gmail.com',
                    emailShow: false,
                    person: staffYang
            )
            AuthUser sa = new AuthUser(passwd: 'd033e22ae348aeb5660fc2140aec35850c4da997',
                    username: 'hudan',
                    userRealName: '胡丹',
                    enabled: true,
                    email: 'liuchao36@gmail.com',
                    emailShow: false,
                    person: staffHu
            )
            AuthUser customerHubiqiu = new AuthUser(passwd: 'd033e22ae348aeb5660fc2140aec35850c4da997',
                    username: 'hubiqiu',
                    userRealName: '胡碧秋',
                    enabled: true,
                    email: 'liuchao36@gmail.com',
                    emailShow: false,
                    person: customer2
            )
            AuthUser customerLiuchao = new AuthUser(passwd: 'd033e22ae348aeb5660fc2140aec35850c4da997',
                    username: 'liuchao',
                    userRealName: '刘超',
                    enabled: true,
                    email: 'liuchao36@gmail.com',
                    emailShow: false,
                    person: customer1
            )

            adminRole.save()
            managerRole.save()
            saRole.save()
            customerRole.save()

            admin.addToAuthorities(adminRole)
            manager.addToAuthorities(managerRole)
            sa.addToAuthorities(saRole)
            customerHubiqiu.addToAuthorities(customerRole)
            customerLiuchao.addToAuthorities(customerRole)

            admin.save()
            manager.save()
            sa.save()
            customerHubiqiu.save()
            customerLiuchao.save()

            JobItem jobItem1_1 = new JobItem(staff: staffHu, product: photoPrint1, quantity: 30, price:photoPrint1.currentPrice)
            Job job1 = new Job(date:new Date(), customer: customer2)
            job1.addToJobItems(jobItem1_1)


            JobItem jobItem2_1 = new JobItem(staff: staffHu, product: photoPrint2, quantity: 30, price:photoPrint2.currentPrice)
            Job job2 = new Job(date:new Date(), customer: customer2)
            job2.addToJobItems(jobItem2_1)

            job1.save()
            job2.save()

            job1.actualValue = job1.value
            job2.actualValue = job2.value

            job1.save()
            job2.save()

        }

    }

    def saveModel = {
        it.save()
        if (it.hasErrors()) {
            it.errors.each {error ->
                println error
            }
        }
    }

    def destroy = {
    }
} 